using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Mail;
using System.Collections;

public  class GoogleMail
{
    public static String From = "infor.msmart@gmail.com";
    public static String Password = "nguyenthien321";

    

    public void Send(String To, String Subject, String Body)
    {
        MailMessage Mail = GoogleMail.CreateMailMessage(To, Subject, Body);
        
        GoogleMail.Send(Mail);
    }

     public void Send(String To, String Cc, String Bcc, String Subject, String Body, String[] Attachments)
    {
        MailMessage Mail = GoogleMail.CreateMailMessage(To, Subject, Body);

        if (!String.IsNullOrEmpty(Cc))
        {
            Mail.Cc = Cc.Replace(',', ';').Replace(" ", "");
        }

        if (!String.IsNullOrEmpty(Bcc))
        {
            Mail.Bcc = Bcc.Replace(',', ';').Replace(" ", "");
        }

        if (Attachments != null)
        {
            foreach (String Attachment in Attachments)
            {
                Mail.Attachments.Add(Attachment);
            }
        }

        GoogleMail.Send(Mail);
    }

    static MailMessage CreateMailMessage(String To, String Subject, String Body)
    {
        MailMessage Mail = new MailMessage();
        try
        {
            Mail.BodyFormat = MailFormat.Html;
            Mail.BodyEncoding = System.Text.Encoding.UTF8;
            Mail.From = From;
            Mail.To = To;
            Mail.Subject = Subject;
            Mail.Body = Body;
            
            Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", "smtp.gmail.com");
            Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
            Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "2");
            Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
            Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", GoogleMail.From);
            Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", GoogleMail.Password);
            Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");

           

        }
        catch (Exception) { }
        return Mail;
    }

    static void Send(MailMessage Mail)
    {
        try
        {
            SmtpMail.SmtpServer = "smtp.gmail.com:465";
            SmtpMail.Send(Mail);
        }
        catch (Exception) { }
    }
}
